package com.ancun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class RedisController {
    // RedisTemplate使用的是JdkSerializationRedisSerializer
    // 字符串会出现: \xAC\xED\x00\x05t\x00\x04
    // 用StringRedisTemplate就没有了
    // @Autowired
    // RedisTemplate redisTemplate;

    // StringRedisTemplate 使用的是StringRedisSerializer
    @Autowired
    StringRedisTemplate redisTemplate;

    @RequestMapping(value="/set")
    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
        System.out.println("set");
    }

    @RequestMapping(value="/setifabsent")
    public boolean setIfAbsent(String key, String value) {
        return redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    @RequestMapping(value="/get")
    public String get(String key)
    {
        return redisTemplate.opsForValue().get(key).toString();
    }

    @RequestMapping(value="/put")
    public void put(String key, String hashKey, String value)
    {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    @RequestMapping(value="/entries")
    public Object entries(String key)
    {
        return redisTemplate.opsForHash().entries(key);
    }

    @RequestMapping(value="/delete")
    public Long delete(String key, String hashKey)
    {
        return redisTemplate.opsForHash().delete(key, hashKey);
    }
}
